package com.ms.education.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.course.entity.Classroom;
import com.ms.education.course.entity.PushClass;
import com.ms.education.course.entity.vo.PageClassVo;
import com.ms.education.course.mapper.PushClassMapper;
import com.ms.education.course.service.ClassroomService;
import com.ms.education.course.service.PushClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-03-06
 */
@Service
public class PushClassServiceImp extends ServiceImpl<PushClassMapper, PushClass> implements PushClassService {

    @Autowired
    private ClassroomService classroomService;

    @Override
    public Map<String, Object> getClassNotInPush(long current, long limit, Integer pushId, PageClassVo pageClassVo) {
        QueryWrapper<PushClass> wrapper = new QueryWrapper<PushClass>().eq("push_id", pushId).select("class_id");
        List<Integer> classIds = list(wrapper).stream().map(PushClass::getClassId).collect(Collectors.toList());
        pageClassVo.setExcludes(classIds);
        Page<Classroom> page = classroomService.pageClass(current, limit, pageClassVo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows", page.getTotal());
        map.put("classrooms", page.getRecords());
        return map;
    }

    @Override
    public void deleteClassPush(Integer classId, Integer pushId) {
        UpdateWrapper<PushClass> wrapper = new UpdateWrapper<PushClass>().eq("class_id", classId).eq("push_id", pushId);
        remove(wrapper);
    }
}
